﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using Excel = Microsoft.Office.Interop.Excel;

namespace EventRecord
{
    public partial class EventLogForm : Form
    {
        public EventLogForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                this.listView1.Items.Clear();
                if (!EventLog.SourceExists("TJ_EMS_EventLog", "."))
                {
                    EventLog.CreateEventSource(new EventSourceCreationData("TJ_EMS_EventLog", "Application"));
                }
                //eventLog.Clear();
                EventLog.WriteEntry("TJ_EMS_EventLog", "日志观察器已启动", EventLogEntryType.Information);
                EventLog.WriteEntry("TJ_EMS_EventLog", "工程管理器已启动", EventLogEntryType.Information);
            }
            catch (Exception)
            {

            }

        }
        private void Form1_Activated(object sender, EventArgs e)
        {
            try
            {
                display_event();
            }
            catch (Exception)
            {

            }
        }

        private void display_event()
        {
            this.listView1.Items.Clear();
            EventLog eventLog = new EventLog("Application", ".", "TJ_EMS_EventLog");
            EventLogEntryCollection MyEventCollection = eventLog.Entries;
            int Count = MyEventCollection.Count;
            this.groupBox1.Text = "显示我的日志(共" + Count.ToString() + ")个事件";
            for (int i = 0; i < Count; i++)
            {
                if (MyEventCollection[Count - i - 1].Source != "TJ_EMS_EventLog")
                {
                    continue;
                }
                EventLogEntry MyEntry = MyEventCollection[Count - i - 1];

                string[] SubItems ={ MyEntry.EntryType.ToString(),
                                    MyEntry.TimeGenerated.ToLongDateString(),
                                    MyEntry.TimeGenerated.ToLongTimeString(),
                                    MyEntry.Message,
                                    MyEntry.UserName,
                                    MyEntry.MachineName
                                  };
                ListViewItem MyItem = new ListViewItem(SubItems);
                this.listView1.Items.Add(MyItem);
            }

        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 导出日志文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Excel.ApplicationClass MyExcel = new Excel.ApplicationClass();
            MyExcel.Visible = true;
            if (MyExcel == null)
            {
                MessageBox.Show("EXCEL无法启动！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int rowcount = 0;
            int columncount = 0;
            MyExcel.Application.Workbooks.Add(true);
            columncount = this.listView1.Columns.Count;

            rowcount = this.listView1.Items.Count;

            for (int i = 0; i < rowcount; i++)
            {
                for (int j = 0; j < columncount; j++)
                {

                    MyExcel.Cells[i + 1, j + 1] = this.listView1.Items[i].SubItems[j].Text;
                }
            }
        }

        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EventLog eventLog = new EventLog("Application", ".", "TJ_EMS_EventLog");
            eventLog.Clear();
            display_event();
        }

    }


}